package com.iwop.moto.controller;

import com.alibaba.fastjson.JSON;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.MBrokerage;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.User;
import com.iwop.moto.service.MBrokerageService;
import com.iwop.moto.service.PromotionService;
import com.iwop.moto.service.SMSService;
import com.iwop.moto.service.UserService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mobom on 2016/12/14.
 */

@RequestMapping("brokerage")
@Api(value = "brokerage", description = "佣金")
@Controller
public class MBrokerageController extends BaseController {

    @Autowired
    private MBrokerageService mBrokerageService;
    @Autowired
    private UserService userService;
    @Autowired
    private PromotionService promotionService;
    @RequestMapping(value = "/store/list",method = RequestMethod.GET)
    @ResponseBody
    public ResultModel brokerageList(@ApiParam(name = "store_id", value = "店铺id", defaultValue = "")
                                     @RequestParam(value = "store_id") String store_id,
                                     @ApiParam(name = "session_id", value = "用户id", defaultValue = "")
                                     @RequestParam(value = "session_id") String session_id) {
          try {
              List<MBrokerage> list=mBrokerageService.findBrokerageList(session_id);
              User userBySid = userService.getUserBySid(session_id);
              int counts = promotionService.countFromUserByHostId(session_id);
              Map<String,Object> map=new HashMap<String,Object>();
              String redPacket = userBySid.getRedPacket()+"";
              map.put("brokerage", redPacket);
              map.put("countPeople",counts);
              map.put("brokerageList",list);
              ResultModel resultModel=new ResultModel();
              resultModel.setData(JSON.toJSONString(map));
              resultModel.setCode(1);
              return resultModel;
          }catch (Exception e){
            e.printStackTrace();
              return getErr(ErrCode.ERROR_SYSTEM);
          }
    }

    @RequestMapping(value = "/store/detail",method = RequestMethod.GET)
    @ResponseBody
    public ResultModel brokerageDetail(@ApiParam(name = "sid", value = "店铺id", defaultValue = "")
                                     @RequestParam(value = "sid") String sid) {
         try {
             MBrokerage mBrokerage=mBrokerageService.findOneBySid(sid);
             return getSucceed(mBrokerage,ErrCode.SUCCEED);
         }catch (Exception e){
           return getErr(ErrCode.ERROR_SYSTEM);
         }
    }
}
